package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.*;
import com.jy.wms.service.WmsGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: WmsGoodsController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author JaneLiu
 * @Date Fri Sep 01 11:00:18 CST 2017
 */
@Controller
@RequestMapping("/wmsgoods")
@Api(value = "/wmsgoods", description = "关于货品管理的操作")
public class WmsGoodsController extends BaseController<WmsGoodsService,WmsGoods> {


//    @RequestMapping("/toWmsGoods")
//    public String toWmsGoods() {
//        return "wmsgoods/wmsgoods";
//    }

    @ApiOperation(value = "货品管理 POST请求查询当前货主的货品信息",response = JSONObject.class, notes = "POST请求查询货品信息")
    @PostMapping(value="/list")
    public @ResponseBody JSONObject getWmsGoods(@RequestBody WmsGoods entity, HttpSession session) {
        Page<WmsGoods> list = new Page<WmsGoods>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            entity.setHzid(hz.get(0).getHzid());
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            entity.setCkid(userinfo.getUser().getOrgId().get(0).toString());
            list=baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        }catch (ServiceException e) {
            e.printStackTrace();
        }
        return json;
    }

//    @RequestMapping(value = "/delete",method = RequestMethod.POST)
//    @ApiOperation(value = "货品管理 多个或单个货品信息",response = JSONObject.class, notes = "POST删除多个或单个货品信息")
//    public @ResponseBody JSONObject deleteWmsGoods(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) {
//
//        JSONObject json = new JSONObject();
//        try {
//            Integer flag=baseService.baseDeleteBathPrimaryKeys(setModel.getIds(),getSessionDB());
//            if(flag == 0){
//                json = SetStatus.setFunctionType(1,"warning","货品管理","删除货品信息失败","");
//            }else{
//                json = SetStatus.setFunctionType(1,"success","货品管理","删除货品信息成功","");
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//            json = SetStatus.setFunctionType(1,"error","货品管理","删除货品信息失败","");
//        }
//        return json;
//    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "货品管理 新增货品信息",response = JSONObject.class, notes = "POST新增货品信息")
    public @ResponseBody JSONObject addWmsGoods(@RequestBody Map<String,Object> map, HttpSession session) {
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String bh="";
            if(map.containsKey("bh")){
                bh=map.get("bh").toString();
            }
            Integer bhJl=baseService.baseSelectGoodsBh(bh,"");
            if(bhJl==0){
                baseService.insertByProcedure(map,userinfo.getUser().getOrgId().get(0).toString(),hz.get(0).getHzid());
                map.remove("ret");
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"新增货品信息成功","", JSONObject.fromObject(map).toString());
            }else{
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"货品编号已经存在","","");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增货品信息失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"新增货品信息失败",e.getMessage(),"");
        }
    }

    @GetMapping(value = "getWmsGoods/{id}")
    @ApiOperation(value = "货品管理 查看单个货品的信息",response = JSONObject.class, notes = "GET查看单个货品的信息")
    public @ResponseBody
    JSONObject getWmsGoodsById(@PathVariable("id") String id) {
        JSONObject json =getJSONObject();
        JSONObject jb=new JSONObject();
        try {
            jb=baseService.baseSelectByPrimaryKey(id);
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "查看单个货品成功", "", jb.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "edit")
    @ApiOperation(value = "货品管理 修改货品信息",response = JSONObject.class, notes = "POST修改货品信息")
    public @ResponseBody JSONObject editWmsGoods(@RequestBody Map<String,Object> map, HttpSession session) {
        map.put("gxsjs",new Date());
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);

            String bh="";
            if(map.containsKey("bh")){
               bh=map.get("bh").toString();
            }
            Integer bhJl=baseService.baseSelectGoodsBh(bh,map.get("id").toString());
            if(bhJl==0){
                baseService.editByProcedure(map,userinfo.getUser().getOrgId().get(0).toString(),hz.get(0).getHzid());
                map.remove("ret");
                json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"修改货品信息成功","",JSONObject.fromObject(map).toString());
            }else{
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"货品编号已经存在","","");
            }
        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货品信息失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"修改货品信息失败",e.getMessage(),"");
        }
        return json;
    }
    @GetMapping(value = "getAccessoriesByHpid/{hpid}")
    @ApiOperation(value = "货品管理 查看货品绑定配件信息",response = JSONObject.class, notes = "GET查看货品绑定配件信息")
    public @ResponseBody
    JSONObject getAccessoriesByHpid(@PathVariable("hpid") String hpid, HttpSession session) {
        JSONObject json =getJSONObject();

        List<WmsGoodsAccessories> list =  new ArrayList<WmsGoodsAccessories>();
        try {
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            list=baseService.selectAccessoriesByHpid(hpid);
            for(WmsGoodsAccessories a:list){
                a.setHzmc(hz.get(0).getShow_hzid());
            }
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @GetMapping(value = "getSuppliersByHpid/{hpid}")
    @ApiOperation(value = "货品管理 查看货品绑定供应商信息",response = JSONObject.class, notes = "GET查看货品绑定供应商信息")
    public @ResponseBody JSONObject getSuppliersByHpid(@PathVariable("hpid") String hpid, HttpSession session) {
        JSONObject json =getJSONObject();
        List<WmsGoodsSuppliers> list =  new ArrayList<WmsGoodsSuppliers>();

        try {

            list=baseService.selectSuppliersByHpid(hpid);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "getGoodsBySshzid")
    @ApiOperation(value = "货品管理 查看货主下对应的所有货品",response = JSONObject.class, notes = "查看货主下对应的所有货品")
    public @ResponseBody JSONObject getGoodsBySshzid(@RequestBody Map<String,Object> map, HttpSession session) {
        JSONObject json =getJSONObject();
        Page<WmsGoods> list =  new Page<WmsGoods>();
        try {
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);

            String bh="";
            if(map.containsKey("bh")){
                bh=map.get("bh").toString();
            }
            String hpid="";
            if(map.containsKey("hpid")){
                hpid=map.get("hpid").toString();
            }
            list=baseService.baseSelectGoodsBySshzid(hz.get(0).getHzid(),hpid,bh);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.getTotal());
        } catch (Exception e){
            e.printStackTrace();
        }
        return json;
    }
    @PostMapping(value = "goodsBhDuplicateRepeat")
    @ApiOperation(value = "货品管理 货品编号去重",response = JSONObject.class, notes = "货品编号去重")
    public @ResponseBody JSONObject goodsBhDuplicateRepeat(@RequestBody Map<String,Object> map) {
        try {
//            String owner=getSessionInfo(SessionInfo.SessionConsignor).toString();
            String bh="";
            if(map.containsKey("bh")){
                bh=map.get("bh").toString();
            }
            Integer bhJl=baseService.baseSelectGoodsBh(bh,"");

            if(bhJl<=0){
                return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"货品编号无重复，可用","","");
            }else{
                return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"货品编号已经存在","","");
            }
        } catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"货品编号已经存在",e.getMessage(),"");

        }
    }
    @PostMapping(value = "getGoodsBh")
    @ApiOperation(value = "货品管理 货品编号去重",response = JSONObject.class, notes = "获取货品编号")
    public @ResponseBody JSONObject getGoodsBh(){
        try {
            String bh=baseService.baseSelectGoodsBhMax();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"获取货品编号成功","",bh);
        } catch (ServiceException e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"获取货品编号失败",e.getMessage(),"");
        }
    }
    @ApiOperation(value = "货品管理 POST查询库位组信息",response = JSONObject.class, notes = "POST查询库位组信息")
    @PostMapping(value="/seachLibrarypositiongroup")
    public @ResponseBody JSONObject seachLibrarypositiongroup(@RequestBody Map<String,Object> map, HttpSession session) {
        List<WmsLibrarypositiongroup> list = new Page<WmsLibrarypositiongroup>();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String sshzid="";
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=userinfo.getUser().getOrgId().get(0).toString();
            String mc="";
            if(map.containsKey("mc")){mc=map.get("mc").toString();}
            list=baseService.seachLibrarypositiongroup(map.get("id").toString(),sshzid,ckid,mc);

            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @ApiOperation(value = "货品管理 POST添加库位组绑定货品信息",response = JSONObject.class, notes = "POST添加库位组绑定货品信息")
    @PostMapping(value="/addLibrarypositiongroupBindingGoods")
    public @ResponseBody JSONObject addLibrarypositiongroupBindingGoods(@RequestBody Map<String,Object> map, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String sshzid="";
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=userinfo.getUser().getOrgId().get(0).toString();
            List<String> ids=null;
            if(map.containsKey("ids")){ids=JSONArray.fromObject(map.get("ids"));}
            String hpid="";
            if(map.containsKey("hpid")){hpid=map.get("hpid").toString();}
            baseService.addLibrarypositiongroupBindingGoods(ids,hpid,sshzid,ckid);
            json= SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"添加库位组绑定货品信息成功","","");
        }catch (SucessException e) {
            SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"添加库位组绑定货品信息成功","","");
        }catch (ServiceException e) {
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"添加库位组绑定货品信息失败",e.getMessage(),"");
        }catch (Exception e) {
         json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"添加库位组绑定货品信息失败",e.getMessage(),"");
        }
        return json;
    }
    @ApiOperation(value = "货品管理 GET查询货品绑定库位组信息",response = JSONObject.class, notes = "GET查询货品绑定库位组信息")
    @GetMapping(value="/seachGoodsBindingLibrarypositiongroup/{hpid}")
    public @ResponseBody JSONObject seachGoodsBindingLibrarypositiongroup(@PathVariable String hpid, HttpSession session) {
        List<WmsGoodsLocation> list = new Page<WmsGoodsLocation>();
        JSONObject json = getJSONObject();
        try {
            list=baseService.seachGoodsBindingLibrarypositiongroup(hpid);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @ApiOperation(value = "货品管理 POST删除货品绑定库位组",response = JSONObject.class, notes = "POST删除货品绑定库位组")
    @PostMapping(value="/delectGoodsBindingLibrarypositiongroup")
    public @ResponseBody JSONObject delectGoodsBindingLibrarypositiongroup(@ApiParam(required = true, name = "setModel", value = "配置参数映射对象") @RequestBody SetAndModifyModel setModel) {
        JSONObject json=new JSONObject();
        try {
            List<String> ids=JSONArray.fromObject(setModel.getIds());
            baseService.delectGoodsBindingLibrarypositiongroup(ids);
            json= SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"删除货品绑定库位组成功","","");
        }catch (SucessException e) {
            SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"删除货品绑定库位组成功","","");
        }catch (ServiceException e) {
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"删除货品绑定库位组失败",e.getMessage(),"");
        }catch (Exception e) {
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"删除货品绑定库位组失败",e.getMessage(),"");
        }
        return json;
    }
    @ApiOperation(value = "货品管理 POST更新货品库位组绑定序号",response = JSONObject.class, notes = "POST更新货品库位组绑定序号")
    @PostMapping(value="/updateGoodsBindingLibrarypositiongroup")
    public @ResponseBody JSONObject updateGoodsBindingLibrarypositiongroup( @RequestBody List<WmsGoodsLocation> locations) {
        JSONObject json=new JSONObject();
        try {
            baseService.updateGoodsBindingLibrarypositiongroup(locations);
            json= SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"更新货品库位组绑定序号成功","","");
        }catch (SucessException e) {
            SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"更新货品库位组绑定序号成功","","");
        }catch (ServiceException e) {
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"更新货品库位组绑定序号失败",e.getMessage(),"");
        }catch (Exception e) {
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"更新货品库位组绑定序号失败",e.getMessage(),"");
        }
        return json;
    }
    @ApiOperation(value = "货品管理 POST查询货主信息",response = JSONObject.class, notes = "POST查询货主信息")
    @PostMapping(value="/seachOwnerXx")
    public @ResponseBody JSONObject seachOwnerXx(HttpSession session) {
       WmsOwner list = new WmsOwner();
        JSONObject json = getJSONObject();
        try {
            //获取当前操作用户的货主id
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            String sshzid="";
            if(hz.size()>0){
                sshzid=hz.get(0).getHzid();
            }
            //获取仓库Id
            UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid=userinfo.getUser().getOrgId().get(0).toString();
            list=baseService.seachOwnerXx(sshzid,ckid);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),1);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }
    @ApiOperation(value = "货品管理 GET查询库位类型信息",response = JSONObject.class, notes = "GET查询库位类型信息")
    @GetMapping(value="/seachKwlx/{lx}")
    public @ResponseBody JSONObject getWmsGoods(@RequestParam String lx, HttpSession session) {
        List<SysDictionary> list=new ArrayList<>();
        JSONObject json = getJSONObject();
        try {
            list=baseService.seachKwlx(lx);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(),(int) list.size());
        }catch (ServiceException e) {
            e.printStackTrace();
        }
        return json;
    }
    @GetMapping(value = "lockGoods/{id}")
    @ApiOperation(value = "货品管理 货品变为无效",response = JSONObject.class, notes = "POST修改货品无效")
    public @ResponseBody JSONObject lockGoods(@PathVariable String id, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
                WmsGoods goods = new WmsGoods();
                goods.setId(id);
                goods.setSfyx("0");
                baseService.baseUpdateByPrimaryKeySelective(goods);
                json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"锁定货品信息成功","","");
        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"锁定货品信息失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"锁定货品信息失败",e.getMessage(),"");
        }
        return json;
    }
    @GetMapping(value = "unlockGoods/{id}")
    @ApiOperation(value = "货品管理 货品变为有效",response = JSONObject.class, notes = "POST修改货品有效")
    public @ResponseBody JSONObject unlockGoods(@PathVariable String id, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            WmsGoods goods = new WmsGoods();
            goods.setId(id);
            goods.setSfyx("1");
            baseService.baseUpdateByPrimaryKeySelective(goods);
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"解锁货品信息成功","","");
        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"解锁货品信息失败",e.getMessage(),"");
        }catch (Exception e){
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"解锁货品信息失败",e.getMessage(),"");
        }
        return json;
    }
    @ApiOperation(value = "货品管理 解除绑定",response = JSONObject.class, notes = "解除绑定库位")
    @PostMapping(value="/cleanLibraryposition")
    public @ResponseBody JSONObject cleanLibraryposition(@RequestBody Map<String,Object> map) {
        JSONObject json=new JSONObject();
        try {

            baseService.cleanLibraryposition(map.get("hpid").toString(), map.get("lx").toString(), map.get("kwid").toString());
            json= SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(),"解除绑定库位成功","","");
        }catch (ServiceException e) {
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"解除绑定库位失败",e.getMessage(),"");
        }catch (Exception e) {
            json= SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(),"解除绑定库位失败",e.getMessage(),"");
        }
        return json;
    }
    @ApiOperation(value = "导入货品信息", response = JSONObject.class, notes = "导入货品信息")
    @PostMapping(value = "importGoodsExcle")
    public @ResponseBody
    JSONObject importGoodsExcle(HttpSession session, @RequestBody String filepath) {
        try {

            baseService.importGoodsExcle(session,filepath);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "导入货品信息成功", "", "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "导入货品信息失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "导入货品信息失败", e.getMessage(), "");
        }
    }
}
